<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Receives data from a connected socket</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.socket-read.html">socket_read</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.socket-recvfrom.html">socket_recvfrom</a></div>
 <div class="up"><a href="ref.sockets.html">Socket 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.socket-recv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_recv</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5)</p><p class="refpurpose"><span class="refname">socket_recv</span> &mdash; <span class="dc-title">Receives data from a connected socket </span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.socket-recv-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>socket_recv</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$buf</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$len</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code></span>
   )</div>

  <p class="para rdfs-comment">
   The <span class="function"><strong>socket_recv()</strong></span> function receives
   <em><code class="parameter">len</code></em> bytes of data in <em><code class="parameter">buf</code></em> from
   <em><code class="parameter">socket</code></em>. <span class="function"><strong>socket_recv()</strong></span> can
   be used to gather data from connected sockets. Additionally, one or
   more flags can be specified to modify the behaviour of the
   function.
  </p>
  <p class="para">
   <em><code class="parameter">buf</code></em> is passed by reference, so it must be
   specified as a variable in the argument list. Data read from
   <em><code class="parameter">socket</code></em> by <span class="function"><strong>socket_recv()</strong></span>
   will be returned in <em><code class="parameter">buf</code></em>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-recv-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">socket</code></em></dt>

     <dd>

      <p class="para">
       The <em><code class="parameter">socket</code></em> must be a socket resource previously
       created by socket_create().
      </p>
     </dd>

    

    
     <dt>
<em><code class="parameter">buf</code></em></dt>

     <dd>

      <p class="para">
       The data received will be fetched to the variable specified with
       <em><code class="parameter">buf</code></em>. If an error occurs, if the
       connection is reset, or if no data is
       available, <em><code class="parameter">buf</code></em> will be set to <strong><code>NULL</code></strong>.
      </p>
     </dd>

    

    
     <dt>
<em><code class="parameter">len</code></em></dt>

     <dd>

      <p class="para">
       Up to <em><code class="parameter">len</code></em> bytes will be fetched from remote host.
      </p>
     </dd>

    

    
     <dt>
<em><code class="parameter">flags</code></em></dt>

     <dd>

      <p class="para">
       The value of <em><code class="parameter">flags</code></em> can be any combination of 
       the following flags, joined with the binary OR (<em>|</em>)
       operator.
      </p>
      
      <table class="doctable table">
       <caption><strong>Possible values for <em><code class="parameter">flags</code></em></strong></caption>
       
        <thead>
         <tr>
          <th>Flag</th>
          <th>Description</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td><strong><code>MSG_OOB</code></strong></td>
          <td>
           Process out-of-band data.
          </td>
         </tr>

         <tr>
          <td><strong><code>MSG_PEEK</code></strong></td>
          <td>
           Receive data from the beginning of the receive queue without
           removing it from the queue.
          </td>
         </tr>

         <tr>
          <td><strong><code>MSG_WAITALL</code></strong></td>
          <td>
           Block until at least <em><code class="parameter">len</code></em> are received.
           However, if a signal is caught or the remote host disconnects, the
           function may return less data.
          </td>
         </tr>

         <tr>
          <td><strong><code>MSG_DONTWAIT</code></strong></td>
          <td>
           With this flag set, the function returns even if it would normally
           have blocked.
          </td>
         </tr>

        </tbody>
       
      </table>

     </dd>

        
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.socket-recv-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>socket_recv()</strong></span> returns the number of bytes received,
   or <strong><code>FALSE</code></strong> if there was an error. The actual error code can be retrieved by 
   calling <span class="function"><a href="function.socket-last-error.html" class="function">socket_last_error()</a></span>. This error code may be
   passed to <span class="function"><a href="function.socket-strerror.html" class="function">socket_strerror()</a></span> to get a textual explanation
   of the error.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.socket-recv-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4497">
    <p><strong>Example #1 <span class="function"><strong>socket_recv()</strong></span> example</strong></p>
    <div class="example-contents"><p>
     This example is a simple rewrite of the first example from
     <a href="sockets.examples.html" class="xref">范例</a> to
     use <span class="function"><strong>socket_recv()</strong></span>.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"&lt;h2&gt;TCP/IP&nbsp;Connection&lt;/h2&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Get&nbsp;the&nbsp;port&nbsp;for&nbsp;the&nbsp;WWW&nbsp;service.&nbsp;*/<br /></span><span style="color: #0000BB">$service_port&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getservbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'tcp'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Get&nbsp;the&nbsp;IP&nbsp;address&nbsp;for&nbsp;the&nbsp;target&nbsp;host.&nbsp;*/<br /></span><span style="color: #0000BB">$address&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gethostbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www.example.com'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Create&nbsp;a&nbsp;TCP/IP&nbsp;socket.&nbsp;*/<br /></span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_STREAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_TCP</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"socket_create()&nbsp;failed:&nbsp;reason:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">())&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Attempting&nbsp;to&nbsp;connect&nbsp;to&nbsp;'</span><span style="color: #0000BB">$address</span><span style="color: #DD0000">'&nbsp;on&nbsp;port&nbsp;'</span><span style="color: #0000BB">$service_port</span><span style="color: #DD0000">'..."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$address</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$service_port</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"socket_connect()&nbsp;failed.\nReason:&nbsp;(</span><span style="color: #0000BB">$result</span><span style="color: #DD0000">)&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">))&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"HEAD&nbsp;/&nbsp;HTTP/1.1\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Host:&nbsp;www.example.com\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Connection:&nbsp;Close\r\n\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Sending&nbsp;HTTP&nbsp;HEAD&nbsp;request..."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">socket_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$in</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$in</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Reading&nbsp;response:\n\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$buf&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'This&nbsp;is&nbsp;my&nbsp;buffer.'</span><span style="color: #007700">;<br />if&nbsp;(</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">!==&nbsp;(</span><span style="color: #0000BB">$bytes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_recv</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$buf</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2048</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MSG_WAITALL</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Read&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #DD0000">&nbsp;bytes&nbsp;from&nbsp;socket_recv().&nbsp;Closing&nbsp;socket..."</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"socket_recv()&nbsp;failed;&nbsp;reason:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">))&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$buf&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"OK.\n\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     The above example will produce something like:
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;h2&gt;TCP/IP Connection&lt;/h2&gt;
OK.
Attempting to connect to &#039;208.77.188.166&#039; on port &#039;80&#039;...OK.
Sending HTTP HEAD request...OK.
Reading response:

Read 123 bytes from socket_recv(). Closing socket...HTTP/1.1 200 OK
Date: Mon, 14 Sep 2009 08:56:36 GMT
Server: Apache/2.2.3 (Red Hat)
Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
ETag: &quot;b80f4-1b6-80bfd280&quot;
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8

OK.
</pre></div>
    </div>
   </div>
  </p>
 </div>

                     
</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.socket-read.html">socket_read</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.socket-recvfrom.html">socket_recvfrom</a></div>
 <div class="up"><a href="ref.sockets.html">Socket 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
